<template>
  <div class="menu-management-container">
    <!--查询-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字典标签" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入字典标签"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>


    <el-row :gutter="24">
      <el-col>
        <!--头部-->
        <el-card shadow="hover">
          <vab-query-form>
            <vab-query-form-top-panel :span="12">
              <el-button
                icon="el-icon-plus"
                type="primary"
                @click="handleAdd"
              >
                添加
              </el-button>
            </vab-query-form-top-panel>
          </vab-query-form>

          <!--表格-->
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="name" label="字典标签" width="180">
            </el-table-column>
            <el-table-column prop="value" label="字典值" width="180">
            </el-table-column>
            <el-table-column prop="dict_code" label="字典编码">
            </el-table-column>
            <el-table-column prop="seq" label="排序" width="60">
            </el-table-column>
            <el-table-column prop="remarks" label="备注">
            </el-table-column>
            <!--操作按钮-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            background
            :current-page="queryParams.current"
            layout="total, sizes, prev, pager, next, jumper"
            :page-size="queryParams.pagesize"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </el-card>
      </el-col>
    </el-row>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典编码" prop="dict_code"  >
          <el-input v-model="form.dict_code" :disabled="true" placeholder="请输入字典类型"/>
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称"/>
        </el-form-item>
        <el-form-item label="字典值" prop="value">
          <el-input v-model="form.value" placeholder="请输入分类名称"/>
        </el-form-item>
        <el-form-item label="显示排序" prop="seq">
          <el-input-number v-model="form.seq" controls-position="right" :min="0" />
        </el-form-item>
<!--        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getDictValue, getDictValueByid, addDictValue, deitDictValue, delDictValue} from "@/api/dictManagement";

export default {
  name: "Dict",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 字典表格数据
      tableData: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        dict_code: undefined,
        current: 1,
        pagesize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "字典标签不能为空", trigger: "blur"}
        ],
        code: [
          {required: true, message: "字典类型不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    const code = this.$route.params && this.$route.params.code;
    this.form.dict_code = code
    this.queryParams.dict_code = code
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    async getList() {
      this.tableData = [];
      this.loading = true;
      const {data} = await getDictValue(this.queryParams);
      this.tableData = data.list
      this.total = data.total
      this.loading = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      const code = this.$route.params && this.$route.params.code;
      this.form.dict_code = code
      this.open = true;
      this.title = "添加字典类型";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            deitDictValue(this.form).then(Response => {
              if (Response.status == 200){
                this.$baseMessage(Response.message, 'success', 'vab-hey-message-success')
                this.getList()
                this.open = false;
              }else {
                this.$baseMessage(Response.message, 'error', 'vab-hey-message-success')
                this.open = false;
              }
            })
          } else {
            addDictValue(this.form).then(Response => {
              if (Response.status == 200){
                this.$baseMessage(Response.message, 'success', 'vab-hey-message-success')
                this.getList()
                this.open = false;
              }else {
                this.$baseMessage(Response.message, 'error', 'vab-hey-message-success')
                this.open = false;
              }
            })
          }
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
       getDictValueByid(id).then(response => {
         this.form = response.data;
         this.open = true;
         this.title = "修改字典类型";
       });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$baseConfirm('你确定要删除当前项吗', null, async () => {
        const id = row.id
        delDictValue(id).then(Response => {
          if (Response.status == 200){
            this.$baseMessage(Response.message, 'success', 'vab-hey-message-success')
            this.getList()
            this.open = false;
          }else {
            this.$baseMessage(Response.message, 'error', 'vab-hey-message-success')
            this.open = false;
          }
        })
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dict_code: undefined,
        name: undefined,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getList()
    },
  }
}
</script>
